package com.laolang.gongda.bookshop.service.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.laolang.gongda.bookshop.domain.User;
import com.laolang.gongda.bookshop.service.UserService;

@Transactional
@Service("userService")
public class UserServiceImpl implements UserService{
	
	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public void persist(User user) {
		entityManager.persist(user);
	}

	@Override
	public void remove(User user) {
		entityManager.remove(entityManager.merge(user));
	}

	@Override
	public void merge(User user) {
		entityManager.merge(user);
	}

	@Override
	public User find(Integer id) {
		return entityManager.find(User.class, id);
	}

	@Override
	public List<User> findUserByEmail(String email) {
		String hql = "SELECT o FROM User o WHERE o.email=:email";
		TypedQuery<User> query = entityManager.createQuery(hql, User.class);
		query.setParameter("email", email);
		return query.getResultList();
	}

}
